<style rel="stylesheet/scss" lang="scss" scoped>
</style>

<template>
    <div>
        <div class="container">
            <el-card shadow="hover" style="padding:0">

                <el-form ref="searchForm" :inline="true" :model="searchForm" size="medium" label-width="120px">
                    <el-card class="operate_container" shadow="never">
                        <div class="wrapper">
                            <div><i class="el-icon-tickets" style="margin-right: 10px;"></i>数据列表</div>

                            <!-- 关键字模糊检索 -->
                            <!--
                            <div class="searchKeyword">
                                <el-form-item prop="keyword">
                                    <el-input placeholder="请输入检索关键字" v-model="searchForm.keyword" clearable @clear="handleResetSearch('searchForm')">
                                        <el-button slot="append" icon="el-icon-search" @click="handleSearch()"></el-button>
                                    </el-input>
                                </el-form-item>
                            </div>
                            -->

                            <!-- 操作按钮 -->
                            <div>
                                <el-button type="primary" icon="el-icon-plus" size="medium" circle @click="addOrUpdateVisible"></el-button>
                                <el-button type="danger" icon="el-icon-delete" size="medium" circle @click="handleDelete"></el-button>
                                <el-button type="primary" icon="el-icon-refresh" size="medium" circle @click="handleResetSearch('searchForm')"></el-button>
                                <el-button type="primary" icon="el-icon-search" size="medium" circle @click="searchVisible = !searchVisible"></el-button>
                            </div>
                        </div>
                    </el-card>

                    <!-- 组合条件检索 -->
                    <el-card class="filter_container" shadow="never" v-show="searchVisible">
                        <div style="margin-top: 15px">
                            <el-form-item label="spuId" prop="spuId">
                                <el-input v-model="searchForm.spuId" clearable placeholder="spuId"></el-input>
                            </el-form-item>
                            <el-form-item label="sku名称" prop="skuName">
                                <el-input v-model="searchForm.skuName" clearable placeholder="sku名称"></el-input>
                            </el-form-item>
                            <el-form-item label="sku介绍描述" prop="skuDesc">
                                <el-input v-model="searchForm.skuDesc" clearable placeholder="sku介绍描述"></el-input>
                            </el-form-item>
                            <el-form-item label="所属分类id" prop="cataId">
                                <el-input v-model="searchForm.cataId" clearable placeholder="所属分类id"></el-input>
                            </el-form-item>
                            <el-form-item label="品牌id" prop="brandId">
                                <el-input v-model="searchForm.brandId" clearable placeholder="品牌id"></el-input>
                            </el-form-item>
                            <el-form-item label="默认图片" prop="skuDefaultImg">
                                <el-input v-model="searchForm.skuDefaultImg" clearable placeholder="默认图片"></el-input>
                            </el-form-item>
                            <el-form-item label="标题" prop="skuTitle">
                                <el-input v-model="searchForm.skuTitle" clearable placeholder="标题"></el-input>
                            </el-form-item>
                            <el-form-item label="副标题" prop="skuSubtitle">
                                <el-input v-model="searchForm.skuSubtitle" clearable placeholder="副标题"></el-input>
                            </el-form-item>
                            <el-form-item label="价格" prop="price">
                                <el-input v-model="searchForm.price" clearable placeholder="价格"></el-input>
                            </el-form-item>
                            <el-form-item label="销量" prop="saleCount">
                                <el-input v-model="searchForm.saleCount" clearable placeholder="销量"></el-input>
                            </el-form-item>
                            <el-form-item label="创建时间" prop="ctime">
                                <el-input v-model="searchForm.ctime" clearable placeholder="创建时间"></el-input>
                            </el-form-item>
                            <el-form-item label="最后更新时间" prop="utime">
                                <el-input v-model="searchForm.utime" clearable placeholder="最后更新时间"></el-input>
                            </el-form-item>
                            <el-form-item label=" ">
                                <el-button type="primary" @click="handleSearch()" size="medium">搜索</el-button>
                                <el-button @click="handleResetSearch('searchForm')" size="medium">重置</el-button>
                            </el-form-item>
                        </div>
                    </el-card>
                </el-form>

                <!-- 表格 -->
                <el-table
                    ref="table" size="small" empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit stripe border highlight-current-row
                    v-loading="dataListLoading"
                    :data="dataList"
                    :header-cell-style="{'padding-top': '10px', 'height': '50px', 'color': '#515A6E', 'background-color': '#f8f8f9'}"
                    @selection-change="handleSelection">
                    <el-table-column type="selection" width="55" align="center"></el-table-column>
                    <el-table-column type="index" width="55" align="center"></el-table-column>

                    <el-table-column label="spuId" prop="spuId" align="center"/>
                    <el-table-column label="sku名称" prop="skuName" align="center"/>
                    <el-table-column label="sku介绍描述" prop="skuDesc" align="center"/>
                    <el-table-column label="所属分类id" prop="cataId" align="center"/>
                    <el-table-column label="品牌id" prop="brandId" align="center"/>
                    <el-table-column label="默认图片" prop="skuDefaultImg" align="center"/>
                    <el-table-column label="标题" prop="skuTitle" align="center"/>
                    <el-table-column label="副标题" prop="skuSubtitle" align="center"/>
                    <el-table-column label="价格" prop="price" align="center"/>
                    <el-table-column label="销量" prop="saleCount" align="center"/>
                    <el-table-column label="创建时间" prop="ctime" align="center"/>
                    <el-table-column label="最后更新时间" prop="utime" align="center"/>

                    <!--
                    <el-table-column label="状态" width="80px" align="center">
                        <template slot-scope="scope">
                            <el-tag type="success" size="small" v-if="scope.row.status === true">正常</el-tag>
                            <el-tag type="danger" size="small" v-else>禁用</el-tag>
                        </template>
                    </el-table-column>
                    -->


                    <el-table-column label="操作" width="120px" align="center" fixed="right">
                        <template slot-scope="scope">
                            <div class="table_action">
                                <el-button type="primary" icon="el-icon-edit" circle size="small" @click="addOrUpdateVisible(scope.row)"></el-button>
                                <el-button type="danger" icon="el-icon-delete" circle size="small" @click="handleDelete(scope.row)"></el-button>

                                <!--
                                <el-button type="text" size="small" @click="handleOpenDialog('edit', scope.row)">编辑</el-button>
                                <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>

                                <el-popover
                                    class="table_popover"
                                    placement="bottom"
                                    trigger="hover">
                                    <div class="table_popover_tools">
                                        <el-button type="text" size="small">编辑</el-button>
                                        <el-button type="text" size="small">删除</el-button>
                                    </div>
                                    <el-button icon="el-icon-s-tools" circle slot="reference" size="small"></el-button>
                                </el-popover>

                                <div class="table_action_dropdown">
                                    <el-dropdown split-button type="primary" size="small" style="margin-left: 10px;">
                                        操作
                                        <el-dropdown-menu slot="dropdown">
                                            <el-dropdown-item>黄金糕</el-dropdown-item>
                                            <el-dropdown-item>狮子头</el-dropdown-item>
                                            <el-dropdown-item>螺蛳粉</el-dropdown-item>
                                            <el-dropdown-item>双皮奶</el-dropdown-item>
                                            <el-dropdown-item>蚵仔煎</el-dropdown-item>
                                        </el-dropdown-menu>
                                    </el-dropdown>
                                </div>

                                <div class="table_action_dropdown margin-left-10">
                                    <el-dropdown>
                                        <span class="el-dropdown-link">
                                            下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
                                        </span>
                                        <el-dropdown-menu slot="dropdown">
                                            <el-dropdown-item>黄金糕</el-dropdown-item>
                                            <el-dropdown-item>狮子头</el-dropdown-item>
                                            <el-dropdown-item>螺蛳粉</el-dropdown-item>
                                            <el-dropdown-item disabled>双皮奶</el-dropdown-item>
                                            <el-dropdown-item divided>蚵仔煎</el-dropdown-item>
                                        </el-dropdown-menu>
                                    </el-dropdown>
                                </div>
                                -->

                            </div>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页工具栏 -->
                <div class="pagination">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchForm.page"
                            :page-sizes="[10, 20, 30, 50, 100, 200]"
                            :page-size="searchForm.limit"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="totalPage">
                    </el-pagination>
                </div>

                <!-- 窗口 -->
                <el-dialog
                    :title="!form.id ? '新增' : '修改'"
                    :visible.sync="visible" :close-on-click-modal="false" @close="handleDialogClose">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

                        <el-form-item label="spuId" prop="spuId">
                            <el-input v-model="form.spuId" placeholder="spuId" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="sku名称" prop="skuName">
                            <el-input v-model="form.skuName" placeholder="sku名称" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="sku介绍描述" prop="skuDesc">
                            <el-input v-model="form.skuDesc" placeholder="sku介绍描述" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="所属分类id" prop="cataId">
                            <el-input v-model="form.cataId" placeholder="所属分类id" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="品牌id" prop="brandId">
                            <el-input v-model="form.brandId" placeholder="品牌id" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="默认图片" prop="skuDefaultImg">
                            <el-input v-model="form.skuDefaultImg" placeholder="默认图片" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="标题" prop="skuTitle">
                            <el-input v-model="form.skuTitle" placeholder="标题" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="副标题" prop="skuSubtitle">
                            <el-input v-model="form.skuSubtitle" placeholder="副标题" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="价格" prop="price">
                            <el-input v-model="form.price" placeholder="价格" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="销量" prop="saleCount">
                            <el-input v-model="form.saleCount" placeholder="销量" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="创建时间" prop="ctime">
                            <el-input v-model="form.ctime" placeholder="创建时间" style="width: 80%;"></el-input>
                        </el-form-item>
                        <el-form-item label="最后更新时间" prop="utime">
                            <el-input v-model="form.utime" placeholder="最后更新时间" style="width: 80%;"></el-input>
                        </el-form-item>

                        <!--
                        <el-select style="width: 100%;" v-model="form.sex" placeholder="员工性别">
                            <el-option label="男" :value="1"></el-option>
                            <el-option label="女" :value="2"></el-option>
                        </el-select>
                        <el-form-item label="排序" prop="sort">
                            <el-input-number v-model="form.sort" :min="1" :max="10000" placeholder="排序"></el-input-number>
                        </el-form-item>
                        <el-form-item label="备注" prop="remarks">
                            <el-input type="textarea" :rows="2" placeholder="请输入内容" style="width: 80%;" v-model="form.remarks"></el-input>
                        </el-form-item>
                        <el-form-item label="状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio :label="true" border>正常</el-radio>
                                <el-radio :label="false" border>锁定</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="服务保证" prop="status">
                           <el-radio-group v-model="form.promotionType" size="small">
                                <el-radio-button :label="0">无优惠</el-radio-button>
                                <el-radio-button :label="1">特惠促销</el-radio-button>
                                <el-radio-button :label="2">会员价格</el-radio-button>
                                <el-radio-button :label="3">阶梯价格</el-radio-button>
                                <el-radio-button :label="4">满减价格</el-radio-button>
                            </el-radio-group>
                        </el-form-item>
                        -->

                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="visible = false">取 消</el-button>
                        <el-button @click="handleSubmit('form')" type="primary" :loading="submitLoading">提 交</el-button>
                    </div>
                </el-dialog>
            </el-card>
        </div>
    </div>
</template>

<script>
import { save, deleted, updated, info, list, page, selectMyPage } from "@/api/product/pmsskuinfo"

export default {
    data() {
        return {
            visible: false,
            searchVisible: false,
            submitLoading: false,

            dataList: [],
            totalPage: 0,
            dataListLoading: true,
            selectionRows: [],

            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: 'ctime,utime',
                sortType: 'desc',
                keyword: null,
                spuId: null,
                skuName: null,
                skuDesc: null,
                cataId: null,
                brandId: null,
                skuDefaultImg: null,
                skuTitle: null,
                skuSubtitle: null,
                price: null,
                saleCount: null,
                ctime: null,
                utime: null
            },
            form: {
                id: null,
                spuId: null,
                skuName: null,
                skuDesc: null,
                cataId: null,
                brandId: null,
                skuDefaultImg: null,
                skuTitle: null,
                skuSubtitle: null,
                price: null,
                saleCount: null,
                ctime: null,
                utime: null
            },
            rules: {
                spuId: [
                    { required: true, message: 'spuId不能为空', trigger: 'blur' }
                ],
                skuName: [
                    { required: true, message: 'sku名称不能为空', trigger: 'blur' }
                ],
                skuDesc: [
                    { required: true, message: 'sku介绍描述不能为空', trigger: 'blur' }
                ],
                cataId: [
                    { required: true, message: '所属分类id不能为空', trigger: 'blur' }
                ],
                brandId: [
                    { required: true, message: '品牌id不能为空', trigger: 'blur' }
                ],
                skuDefaultImg: [
                    { required: true, message: '默认图片不能为空', trigger: 'blur' }
                ],
                skuTitle: [
                    { required: true, message: '标题不能为空', trigger: 'blur' }
                ],
                skuSubtitle: [
                    { required: true, message: '副标题不能为空', trigger: 'blur' }
                ],
                price: [
                    { required: true, message: '价格不能为空', trigger: 'blur' }
                ],
                saleCount: [
                    { required: true, message: '销量不能为空', trigger: 'blur' }
                ],
                ctime: [
                    { required: true, message: '创建时间不能为空', trigger: 'blur' }
                ],
                utime: [
                    { required: true, message: '最后更新时间不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
    	this.$nextTick(function () {
	        this.getDataList()
    	})
    },
    methods: {
        handleSelection(selection) { // 多选
            this.selectionRows = selection
        },
        handleSizeChange(val) { // 每页数量
            this.searchForm.limit = val
            this.getDataList()
        },
        handleCurrentChange(val) { // 页码
            this.searchForm.page = val
            this.getDataList()
        },
        handleResetSearch(name) { // 重置搜索表单
            this.handleReset(name)
            this.getDataList()
        },
        handleReset(name) { // 重置表单
            const refs = this.$refs
            refs[name].resetFields()
        },
        handleSearch() { // 单条件搜索
            this.getDataList()
        },
        handleDialogClose() { //表单窗口关闭
            this.handleReset('form')
        },

		/**
         * 打开表单窗口
         */
		addOrUpdateVisible(row) {
            this.visible = true
            if (row.id) {
                this.getInfo(row.id).then(res => {
                    this.form = Object.assign({}, res.data)
                })
            } else {
                this.form.id = null
            }
        },

		/**
         * 提交表单
         */
        handleSubmit(name) {
        	const refs = this.$refs
            refs[name].validate((valid) => {
                if (valid) {
                    this.submitLoading = true
                    if (!this.form.id) {
                        save(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else if (this.form.id) {
                        updated(this.form).then(res => {
                            if (this.ResultMessage(res)) {
                                this.submitLoading = false
                                this.visible = false
                                this.handleReset('form')
                                this.getDataList()
                            } else {
                                this.submitLoading = false
                            }
                        }).catch(error => {
                            this.submitLoading = false
                            this.$notify({ title: '错误', message: error, type: 'error' });
                        })
                    } else {
                        console.log('未知操作');
                    }
                }
            })
        },
		
		/**
         * 删除
         */
        handleDelete(row) { 
            let ids = []
            if (row.id != undefined) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {    
                this.$confirm('确定要删除？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
                    deleted(ids).then(res => {
                        if (this.ResultMessage(res)) {
                            this.getDataList()
                        } 
                    }).catch(error => {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    })
                }).catch(() => {})
            }
        },

		/**
         * 详细信息
         */
        getInfo(id) {
            if (id !== undefined) {
                return info(id).then(res => {
                    if (this.ResultMessage(res, false)) {
                        return res
                    } else {
                        this.$notify({ title: '错误', message: error, type: 'error' });
                    }
                }).catch(error => {
                    this.$notify({ title: '错误', message: error, type: 'error' });
                })
            }
        },
		
        /**
         * 数据列表
         */
        getDataList() {
            page(this.searchForm).then(res => {
                if (this.ResultMessage(res, false)) {
                    this.dataList = res.data.records
                    this.totalPage = res.data.total
                    this.dataListLoading = false
                } else {
                    this.dataListLoading = false
                }
            }).catch(error => {
                this.dataListLoading = false
                this.$notify({ title: '错误', message: error, type: 'error' });
            })
        }
    }
}
</script>